package com.sou.technologytool.fragment;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.benben.commoncore.utils.StatusBarUtils;
import com.benben.commoncore.utils.StringUtils;
import com.benben.commoncore.utils.StyledDialogUtils;
import com.benben.commoncore.utils.ToastUtils;
import com.sou.technologytool.MyApplication;
import com.sou.technologytool.R;
import com.sou.technologytool.ui.MainActivity;
import com.sou.technologytool.ui.PolicyActivity;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeFragment extends LazyBaseFragments {
    @BindView(R.id.status_bar_view)
    View statusBarView;
    @BindView(R.id.edt_amount)
    EditText edt_amount;
    @BindView(R.id.edt_cycle)
    EditText edt_cycle;
    @BindView(R.id.edt_rate)
    EditText edt_rate;
    @BindView(R.id.tv_repayments)
    TextView tv_repayments;
    @BindView(R.id.tv_monthly)
    TextView tv_monthly;
    @BindView(R.id.tv_total)
    TextView tv_total;
    @BindView(R.id.tv_monthly_interest)
    TextView tv_monthly_interest;
    private int mStatusBarHeight;//获取状态栏高度
    private String type = "";
    private String level = "";
    @Override
    public View bindLayout(LayoutInflater inflater) {
        mRootView = inflater.inflate(R.layout.frag_home, null);
        return mRootView;
    }

    @Override
    public void initView() {
        mStatusBarHeight = StatusBarUtils.getStatusBarHeight(mContext);
        statusBarView.getLayoutParams().height = mStatusBarHeight;
        type = MyApplication.mPreferenceProvider.getUserType();
        level = MyApplication.mPreferenceProvider.getLevel();
        if (TextUtils.isEmpty(type)) {
            Intent intent = new Intent(mContext, PolicyActivity.class);
            intent.putExtra("level",level);
            startActivity(intent);
            mContext.finish();
        }
    }

    @OnClick({R.id.tv_sim})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.tv_sim) {
            endInfo();
        }
    }

    private void endInfo() {
        //贷款金额
        String edtAmount = edt_amount.getText().toString().trim();
        //贷款天数
        String edtCycle = edt_cycle.getText().toString().trim();
        //利率
        String edtRate = edt_rate.getText().toString().trim();
        if (StringUtils.isEmpty(edtAmount)) {
            ToastUtils.show(mContext, "Please enter the correct value");
            return;
        }
        if (StringUtils.isEmpty(edtCycle)) {
            ToastUtils.show(mContext, "Please enter the correct value");
            return;
        }
        if (StringUtils.isEmpty(edtRate)) {
            ToastUtils.show(mContext, "Please enter the correct value");
            return;
        }
        StyledDialogUtils.getInstance().loading(mContext);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                double percentage = Double.parseDouble(edtRate) / 100;
                DecimalFormat df = new DecimalFormat("#.00");
                tv_repayments.setText(df.format(Integer.parseInt(edtAmount) * percentage / 365 * Integer.parseInt(edtCycle))+"");
                tv_monthly.setText(df.format(Integer.parseInt(edtAmount) * percentage / 365 * 30 + Integer.parseInt(edtAmount) /  Integer.parseInt(edtCycle) * 30)+"");
                tv_total.setText(df.format(Integer.parseInt(edtAmount) + Integer.parseInt(edtAmount) * percentage / 365 * Integer.parseInt(edtCycle))+"");
                tv_monthly_interest.setText(df.format(Integer.parseInt(edtAmount)  * percentage / 365 * 30)+"");
                StyledDialogUtils.getInstance().dismissLoading();
            }
        }, 1500);
    }

    @Override
    public void initData() {

    }
}
